<template>
    <div class="region">
        <div class="content">
            <div class="left">地区：</div>
            <ul>
                <li :class="{ active: '' === activeRegion }" @click="changeRegion('')">全部</li>
                <li v-for="item in regionArr" :key="item.id" :class="{ active: item.value === activeRegion }"
                    @click="changeRegion(item.value)">
                    {{ item.name }}
                </li>

            </ul>
        </div>
    </div>
</template>

<script setup lang="ts" name="Region">
import { reqHospitalLevelRegionData } from '@/api/home/index'
import type { HospitalLevelAndRegionArr, HospitalLevelAndRegionResponseData } from '@/api/home/type'
import { ref, onMounted } from 'vue'

// 组件挂载完毕后，立刻查询医院的地区目录信息
onMounted(() => {
    getRegion()
})

const $emit = defineEmits(['getDistrictCode'])

// 获取医院地区目录数据
const getRegion = async () => {
    const res: HospitalLevelAndRegionResponseData = await reqHospitalLevelRegionData('Beijin')
    if (res.code === 200) {
        regionArr.value = res.data
    }
}
// 点击医院地区时，改变当前选中地区
const changeRegion = (value: string) => {
    activeRegion.value = value
    //触发自定义事件，回传最新筛选值
    $emit('getDistrictCode', activeRegion.value)
}

// 定义用于存储地区目录信息的数组
const regionArr = ref<HospitalLevelAndRegionArr>([])

// 定义标记选中地区的数据
const activeRegion = ref<string>('')
</script>

<style scoped lang="scss">
.region {
    margin-top: 10px;

    .content {
        display: flex;
        color: #696868;

        .left {
            margin-right: 10px;
            white-space: nowrap;
        }

        ul {
            display: flex;
            flex-wrap: wrap;

            li {
                margin-right: 10px;
                margin-bottom: 5px;

                &.active {
                    color: #55a6fe;
                }
            }

            li:hover {
                color: #55a6fe;
                cursor: pointer;
            }
        }
    }
}
</style>